/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.activity.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.modules.activity.domain.dto.*;
import com.fzhucc.modules.activity.service.ActivityService;
import com.fzhucc.modules.pms.domain.dto.ProjectQRCodeDto;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2022-11-17
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "活动:管理")
@RequestMapping("/api/activity")
public class ActivityController {

    private final ActivityService activityService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('activity:list')")
    public void exportActivity(HttpServletResponse response, ActivityQueryCriteria criteria) throws IOException {
        activityService.download(activityService.queryAll(criteria), response);
    }

    @Log("小程序查询活动列表")
    @ApiOperation("小程序查询活动列表")
    @AnonymousGetMapping("/list")
    public ResponseEntity<Object> queryList(ActivityQueryCriteria criteria, Pageable pageable){
        criteria.setIsShow(1);
        criteria.setIsDelete(0);
        return new ResponseEntity<>(activityService.queryAll(criteria,pageable), HttpStatus.OK);
    }


    @GetMapping
    @Log("查询活动")
    @ApiOperation("查询活动")
    //@PreAuthorize("@el.check('activity:list')")
    public ResponseEntity<Object> query(ActivityQueryCriteria criteria, Pageable pageable){
        criteria.setIsDelete(0);
        return new ResponseEntity<>(activityService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @Log("小程序查询活动Top")
    @ApiOperation("小程序查询活动Top")
    @AnonymousGetMapping("/wxListTop")
    public ResponseEntity<Object> queryActivityTop(ActivityWxListQueryCriteria criteria){
        return new ResponseEntity<>(activityService.queryActivityTop(criteria), HttpStatus.OK);
    }

    /*@Log("小程序查询活动TopByOrgArea")
    @ApiOperation("小程序查询活动TopByOrgArea")
    @AnonymousGetMapping("/wxListTopByOrgArea")
    public ResponseEntity<Object> wxListTopByOrgArea(ActivityWxListQueryCriteria criteria){
        return new ResponseEntity<>(activityService.wxListTopByOrgArea(criteria), HttpStatus.OK);
    }*/

    /*@Log("查询登录用户报名的活动 返回报名情况 及具体活动")
    @ApiOperation("查询登录用户报名的活动 返回报名情况 及具体活动")
    @GetMapping("/myActivity")
    public ResponseEntity<Object> getActivityUsers(){
        return new ResponseEntity<>(activityService.getActivityUsers(), HttpStatus.OK);
    }*/

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个活动")
    @ApiOperation("查询单个活动")
    public ActivityDto queryActivityById(@PathVariable Integer id) {
        return activityService.findById(id);
    }


    @GetMapping("/queryActivityIsSign")
    @Log("查询单个活动 返回活动 并返回该用户是否报名过此活动 未登录用户则返回活动内容  传活动id")
    @ApiOperation("查询单个活动 返回活动 并返回该用户是否报名过此活动 未登录用户则返回活动内容  传活动id")
    public ActivityIsSignUpDto queryActivityIsSign(@RequestParam Integer id) {
        return activityService.queryActivityIsSign(id);
    }

    @PostMapping
    @Log("新增活动")
    @ApiOperation("新增活动")
    //@PreAuthorize("@el.check('activity:add')")
    public ResponseEntity<Object> createActivity(@Validated @RequestBody ActivityAddDto resources){
        return new ResponseEntity<>(activityService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改活动")
    @ApiOperation("修改活动")
    //@PreAuthorize("@el.check('activity:edit')")
    public ResponseEntity<Object> updateActivity(@Validated @RequestBody ActivityEditDto resources){
        activityService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除活动")
    @ApiOperation("删除活动")
    //@PreAuthorize("@el.check('activity:del')")
    public ResponseEntity<Object> deleteActivity(@RequestBody Integer[] ids) {
        activityService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("上传Banner图")
    @PostMapping(value = "/uploadPhoto")
    public ResponseEntity<Object> uploadPhoto(@RequestParam MultipartFile photo, @RequestParam Integer id){
        return new ResponseEntity<>(activityService.douploadpic(photo,id), HttpStatus.OK);
    }

    @Log("根据活动id查询活动签到记录")
    @ApiOperation("根据活动id查询活动签到记录")
    @AnonymousGetMapping(value = "/getSignInRecordByActivityId")
    public ResponseEntity<Object> getSignInRecordByActivityId(@RequestParam Integer activityId){
        return new ResponseEntity<>(activityService.getSignInRecordByActivityId(activityId), HttpStatus.CREATED);
    }

    @ApiOperation("生成活动签到二维码")
    @Log("生成活动签到二维码")
    @PostMapping(value = "/generateQrCode")
    //@PreAuthorize("@el.check('activity:list')")
    public ActivityBindDto generateQrCode(@RequestParam Integer activityId) {
        return activityService.generateQrCode(activityId);
    }


    @PostMapping("/getQRcode")
    @Log("生成活动二维码 扫描该码进入小程序此页面")
    @ApiOperation("生成活动二维码 扫描该码进入小程序此页面")
    public String getQRcode(@RequestParam Integer activityId){
        return activityService.getQRcode(activityId);
    }
}